<template>
  <div class="edit-dialog">
    <el-dialog :visible.sync="dialog"
               title="个人信息"
               :close-on-click-modal="false"
               :show-close="true"
               width="500px"
               center
               @close="handleClose">
      <div class="content userinfo-content">
        <el-form :model="addFormData"
                 label-position="right"
                 ref="userInfoFormRef"
                 :rules="rules"
                 label-width="auto"
                 class="form">
          <el-form-item prop="name"
                        label="用户名">
            <el-input disabled
                      v-model="addFormData.name"
                      maxlength="30"
                      placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="name"
                        label="邮箱">
            <el-input disabled
                      v-model="addFormData.email"
                      maxlength="30"
                      placeholder="请输入邮箱" />
          </el-form-item>
          <el-form-item prop="displayName"
                        label="昵称">
            <el-input v-model="addFormData.displayName"
                      maxlength="20"
                      placeholder="请输入昵称"></el-input>
          </el-form-item>
          <el-form-item prop="phone"
                        label="手机号">
            <el-input v-model="addFormData.phone"
                      placeholder="请输入手机号码" />
          </el-form-item>
           <el-form-item prop="company"
                        label="单位">
            <el-input v-model="addFormData.company"
                      placeholder="请输入您的单位" />
          </el-form-item>
          <el-form-item prop="title"
                        label="职位">
            <el-input v-model="addFormData.title"
                      placeholder="请输入您的职位" />
          </el-form-item>
          <el-form-item prop="state"
                        label="状态"
                        class="state">
            <el-radio-group v-model="addFormData.state"
                            disabled
                            style="width: 100%">
              <el-radio label="NEW">新注册</el-radio>
              <el-radio label="ACTIVE">激活</el-radio>
              <el-radio label="DISABLE">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="description"
                        label="描述">
            <el-input type="textarea"
                      v-model="addFormData.description"
                      :rows="2"
                      maxlength="80"
                      placeholder="请输入描述" />
          </el-form-item>
        </el-form>
        <el-row style="text-align: center">
          <el-button type="primary"
                     :loading="submitLoading"
                     icon="el-icon-check"
                     @click="submitForm('userInfoFormRef')">保 存</el-button>
          <el-button icon="el-icon-close"
                     @click="handleClose">取 消</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { Message } from "element-ui";
import { getUserInfo, updateUserInfo } from "@/api/user";
export default {
  name: "updateInfo",
  props: {
    showDialog: false
  },
  components: {},
  data() {
    return {
      submitLoading: false,
      addFormData: {},
      rules: {
        displayName: [
          { required: true, message: "请输入昵称", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    dialog: function () {
      return this.showDialog;
    }
  },
  watch: {
    dialog(newVal, oldVal) {
      if (newVal) {
        //this.addFormData = this.formData;
        //this.getMessage()
      }
    }
  },
  mounted() {
    getUserInfo().then(res => {
      this.addFormData = res.value;
    });
  },
  methods: {
    // 取消事件
    handleClose() {
      this.$emit("editUserInfoDialogFun", false);
    },
    submitForm(formName) {
      this.submitLoading = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submitLoading = false;
          const data = {
            ...this.addFormData
            // displayName: this.addFormData.displayName,
            // description: this.addFormData.description
          }
          updateUserInfo(data).then(res => {
            if (res.code == "200") {
              Message.success("修改成功！");
              this.$emit("editUserInfoDialogFun", false);
            }
          });
        } else {
          this.submitLoading = false;
          return false;
        }
      });
    }
  }
};
</script>
<style>
.userinfo-content {
  line-height: 36px;
}
</style>
<style lang="scss" scoped>
.edit-pwd {
  padding-top: 80px;

  .form {
    width: 500px;
    margin: auto;

    .title {
      display: block;
      font-size: 16px;
      // font-weight: bold;
      line-height: 30px;
      color: #212121;
    }

    .phone-num {
      width: 100%;
      text-align: left;
    }

    .code {
      width: 100%;
      display: flex;
    }

    .btn {
      margin: 0 20px;
    }
  }
}
</style>
